<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="$emit('close')">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from "@/api/settings";
import { getProfileById } from "@/api/user";
import { assignRoles } from "@/api/employees";
export default {
  props: {
    roleId: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      roleIds: [], // 保存当前选中的权限列表
      list: [], //角色列表
    };
  },
  created() {
    this.loadRoles();
  },
  methods: {
    async hSubmit() {
      try {
        const res = await assignRoles({
          id: this.roleId,
          roleIds: this.roleIds,
        });
        console.log("assignRoles", res);
        this.$message.success(res.message);
        // 保存数据
        this.loadRoles();
        this.$emit("close");
      } catch (err) {
        console.log("assignRoles", err);
      }
    },
    // 获取角色列表
    async loadRoles() {
      try {
        // 发送请求获取所有的角色  pagesize获取最大的数量
        const res = await getRoles({pagesize:100});
        console.log("getRoles", res);
        // 保存数据
        this.list = res.data.rows;
        const info = await getProfileById(this.roleId);
        console.log("getProfileById", info);
        // 保存数据
        this.roleIds = info.data.roleIds || []
      } catch (err) {
        console.log("getRoles", err);
      }
    },
    closeDialog() {},
  },
};
</script>
